package com.util;

import com.google.common.base.Preconditions;

import java.io.*;
import java.util.Enumeration;
import java.util.Formatter;
import java.util.Objects;
import java.util.Scanner;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

/**
 * 文件操作工具类
 *
 * @author think
 * @version 1.0.0
 */
public class ComFileOperUtil {

    /**
     * 字节流转变为字符流
     *
     * @param inputStream 输入流
     * @throws IOException IO异常
     */
    public static void char2Stream(InputStream inputStream) throws IOException {
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
        String line = "";
        while (Objects.nonNull(line = bufferedReader.readLine())) {
            System.out.println(line);
        }
    }

    /**
     * 向标准设备写入数据
     *
     * @param outputStream 输出流
     */
    public static void print2StandOutputDevice(OutputStream outputStream) {
        PrintWriter printWriter = new PrintWriter(outputStream);
        Formatter formatter = new Formatter();
        formatter.format("%s is the test data!", "dx");
        printWriter.println(formatter.toString());
        printWriter.println("thet  print2StandOutputDevice methord!");
    }

    /**
     * 扫描文件
     *
     * @param file 输入
     * @param <T>  类型
     * @throws FileNotFoundException 异常
     */
    public static <T> void scanFile(T file) throws FileNotFoundException {
        Scanner scanner = null;
        if (file instanceof File) {
            scanner = new Scanner((File) file);
        } else if (file instanceof InputStream) {
            scanner = new Scanner((InputStream) file);
        }
        Preconditions.checkArgument(Objects.nonNull(scanner));
        StringBuilder stringBuilder = new StringBuilder();
        while (scanner.hasNext()) {
            stringBuilder.append(scanner.next());
        }
        System.out.println(stringBuilder.toString());
    }

    /**
     * 读写文件
     *
     * @param inputFileName  输入文件名
     * @param outputFileName 输出文件名
     */
    public static void readAndWriteFile(String inputFileName, String outputFileName) {
        try {
            BufferedReader bufferedReader = new BufferedReader(new FileReader(inputFileName));
            char[] charBuffer = new char[1024];
            int offset = 0;
            int len = 1024;
            int readLen = 0;
            while ((readLen = bufferedReader.read(charBuffer, offset, len)) != -1) {
                BufferedOutputStream bufferedOutputStream =
                        new BufferedOutputStream(new FileOutputStream(outputFileName));
                bufferedOutputStream.write(String.valueOf(charBuffer).getBytes(), offset, readLen);
                offset = offset + readLen;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void zipFileOper(String fileName) {
        try {
            ZipFile zipFile = new ZipFile(fileName);
            Enumeration enumeration = zipFile.entries();
            while (enumeration.hasMoreElements()) {
                ZipEntry zipEntry = (ZipEntry) enumeration.nextElement();
                if (!zipEntry.isDirectory()) {
                    System.out.println(zipEntry.getName());
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
